<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据库操作</title>
<style type="text/css">
h1{ color:red;}
pre{line-height:2;font-size:16px;}
</style>
</head>
<body><%!
private void showUsers(Statement st,JspWriter out){
	try(ResultSet rs=st.executeQuery("select * from users")){
		while(rs.next()){
			String xm=rs.getString("xm");
			int age=rs.getInt("age");
			out.println("<p>    "+xm+","+age+"</p>");
		}
	}catch(Exception e){
		try{
			out.println("<p>   "+e.getMessage()+"</p>");
		}catch(Exception e1){
	    }
     }
}
%>
<h1>数据库操作</h1>
<pre>(1)创建数据库
(2)打开默认的数据库
(3)创建数据库表
(4)插入记录
(5)删除数据库记录
(6)更新数据库记录
(7)查询数据库记录</pre><%
try(Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","12qwas");
		Statement st=cn.createStatement();
		){
	st.executeUpdate("create database if not exists db555");
	out.println("<p>成功创建数据库~</p>");
	st.executeUpdate("use db555");
	out.println("<p>成功选择默认数据库~</p>");
	st.executeUpdate("create table if not exists users(xm varchar(4),age int)");
	out.println("<p>成功创建数据库表users~</p>");
	int count=st.executeUpdate("delete from users");
	out.println("<p>成功删除"+count+"条记录~</p>");
	count=st.executeUpdate("insert into users(xm,age) values('张三',18),('李四',20),('王五',21)");
	out.println("<p>成功插入"+count+"条记录，记录如下：</p>");
	showUsers(st,out);
	count=st.executeUpdate("update users set xm='张三丰',age=81 where xm='张三'");
	out.println("<p>成功更新"+count+"条记录，记录如下：</p>");
	showUsers(st,out);
}catch(Exception e){
	out.println("<p>"+e.getMessage()+"</p>");
}
%>
</body>
</html>